const {getDmg,reduceResistanceK,lvSuppressK,objValueAdd } = require("../libs/apis.js")

const WEAPONS = require("./weapons.js");

const CLIPDATA_TYPE = {
    max:{
        fixedhp: 298.75,
        fixedAtt: 19.45,
        fixdefence: 23.15,
        reaction : 23.31,
        attRate: 5.83, 
        hpRate: 5.83, 
        criticalRate: 3.89,
        criticalStrike: 7.77,
        defRate: 7.29,
        charging: 6.48
    },
    mid:{
        fixedhp: 253.94,   // 209.13  239   268.88   298.75
        fixdefence: 19.675, // 16.2   18.52  20.83  23.15
        fixedAtt: 16.535,   //  13.62   15.56   17.51  19.45
        reaction : 19.815,  // 16.32   18.65  20.98  23.31
        attRate: 4.955,  // 4.08  4.66  5.25  5.83
        hpRate: 4.955,   // 4.08  4.66  5.25  5.83
        criticalRate: 3.305,   //  2.72  3.11   3.5   3.89
        criticalStrike: 6.605,  //  5.44  6.22  6.99  7.77
        defRate: 6.195,   //  5.1  5.83  6.56   7.29
        charging: 5.505  //  4.53   5.18   5.83   6.48
    }
}


const CLIPDATA = CLIPDATA_TYPE.mid


const CONFIG = {
    chance: 20,
    your_lv: 90,
    enemy_lv: 90,
    clips_main: {
        criticalStrike: 62.2,
        dmgRate: 46.6,
        hpRate: 46.6,
    },
    clips: ["hpRate", "charging", "criticalRate", "criticalStrike"],
    clips_initial: [
        ["attRate", "hpRate", "criticalRate", "criticalStrike"],
        ["attRate", "hpRate", "criticalRate", "criticalStrike"],
        ["attRate", "reaction", "criticalRate", "criticalStrike"],
        ["attRate", "hpRate", "criticalRate", "criticalStrike"],
        ["hpRate", "reaction", "attRate", "criticalRate"]
    ],
    reactionK : 1.5,
    buffs: [  // 夜莫万 钟
        // { charging: 20}, // 充能套
        { dmgRate:15, hpRate: 20}, // 2+2
        // { dmgRate:48, jk: 40, admgRate: 32}, // 万叶
        // { mnCrRate:15, mnDmgRate: 60}, // mn
        // { jk: 20, dmgRate: 35}, // 钟离 
        // { hpRate:25}, // 双水
    ]
}

let charaters = {
    units : {
        name: "莱依拉", 
        charging: 100,
        defence: 655.22,
        attRate:0,
        hp:11092,
        base: 216.65,    
        criticalRate: 5,   
        hpRate: 24, 
        criticalStrike: 50,
        skills: {
            a: [
                function(data){},
            ]
        }
    },
}


/**
莱依拉 冰 90级角色数值 血量:11092 双爆5/50  生命:24
e 开盾 每1.5s叠1层特效 套盾的角色放e时叠2层 最多同时存在4层
有4层后 产生4个冰飞弹依次攻击 飞弹全发射完才能重新叠层
10级e倍率 e开启 0.2673 e飞弹0.3074  护盾持续12s 冷却15s 护盾:0.1944hp+2288
13级e倍率 e开启 0.3155 e飞弹0.3628  护盾持续12s 冷却15s 护盾:0.2295hp+2860
v4冷却改为12s 倍率削弱
13级e倍率 e开启 0.27.2 e飞弹0.31.28

q 产生一个球 持续发射造成冰伤 命中叠加e特效层数1层
10级倍率 0.1269 x 生命
13级倍率 0.1498 x 生命  v4=>0.0988
持续12s cd15s 能量60

天赋 e层数叠加护盾强效 每层叠加 6% 最多叠4次
e飞弹伤害值提升 提升值为 hp的 1.5%

命座
  1. 提升护盾吸收量 20%
  2. e飞弹命中恢复1能量
  3. e + 3
  4. e 攒满发射时 普攻重击+伤害值 伤害值提升 莱依拉生命的 5% 改增伤触发一次后0.05s移除 基本上只能触发一次攻击
  5. q + 3
  6. e 和 q 增伤40% e叠层间隔减少20%

2+2(15水伤 20生命) 绿剑 生水爆
  e飞弹伤害 3407  e融化 5502
  q 15675   q融化 25312
  e和q能发射多少次 以及融化占比有待确认
     初步预估 e 1.5s叠一次 q命中叠一次 战场角色放e叠两次 12秒应该能超过16发 这种情况下 eq总伤害也就中等偏下的输出量
    
  定位是盾辅 + 后台补伤 4命的普攻重击附加伤害值 给的伤害值很少且触发后0.05s就移除 鸡肋 
 */
calc(calc_clips_initial());

function calc(normal) {
    let charactersDmg = [];
    let count = 0;
    Object.values(WEAPONS).forEach(weapon => {
        if(weapon.weapontype == "sword") {
            charactersDmg.push(getDMG([charaters.units,weapon,{fixedAtt:311,...CONFIG.clips_main},normal], (data,second) => {
                count++;
                data = objValueAdd([data, ...CONFIG.buffs]);
                data.hp = data.hp*(1 + (data.hpRate||0)/100 + 0.06 + 0) + 4780;
               
                if(weapon.skill) { weapon.skill(data); }
                let slipQadd = data.charging * 0.25
                if(slipQadd > 75) {
                    slipQadd = 75
                }
                slipQadd = 0 //
                
                let eFeiDanDmgZhi = data.hp * 0.015
                let FourMinDmgZhi = data.hp * 0.05
               
                let rea = { reaction:data.reaction, reactionQ:CONFIG.reactionK, reactionDmgAdd:data.reactionDmgAdd || 0 }
    
                let EDmg = getDmg({ data:{...data, 
                }, rate:0.3155}).qw 
                * reduceResistanceK(10-(data.jk||0)) * lvSuppressK(CONFIG.your_lv, CONFIG.enemy_lv, 0); //

                data.dmgRate += 40;
                let EFeiDanDmg = getDmg({ data:{...data, 
                    dmgZhi:(data.dmgZhi||0) + eFeiDanDmgZhi
                }, rate:0.3628}).qw 
                * reduceResistanceK(10-(data.jk||0)) * lvSuppressK(CONFIG.your_lv, CONFIG.enemy_lv, 0); //
                let EFeiDanDmgRea = getDmg({ data:{...data, 
                    dmgZhi:(data.dmgZhi||0) + eFeiDanDmgZhi
                }, rate:0.3628, rea}).qw 
                * reduceResistanceK(10-(data.jk||0)) * lvSuppressK(CONFIG.your_lv, CONFIG.enemy_lv, 0); //
                let QDmg = getDmg({ data:{...data, 
                    dmgZhi:(data.dmgZhi||0) + data.hp * 0.1498
                }, rate:0}).qw 
                * reduceResistanceK(10-(data.jk||0)) * lvSuppressK(CONFIG.your_lv, CONFIG.enemy_lv, 0); //
                let QDmgRea = getDmg({ data:{...data, 
                    dmgZhi:(data.dmgZhi||0) + data.hp * 0.1498
                }, rate:0, rea}).qw 
                * reduceResistanceK(10-(data.jk||0)) * lvSuppressK(CONFIG.your_lv, CONFIG.enemy_lv, 0); //
    
                if(data.phyRate) { // 物理特效伤害
                    data.phyDmg = getDmg({data:{...data, dmgRate:0}, rate:data.phyRate}).qw * reduceResistanceK(10-(data.jk||0)) * lvSuppressK(CONFIG.your_lv, CONFIG.enemy_lv);  // 
                }
    
                let point = EDmg + EFeiDanDmg * 8 + EFeiDanDmgRea * 8 + QDmg*0 + QDmgRea * 10 + (data.phyDmg||0);
                let attack = data.base *(1 + data.attRate/100) + data.fixedAtt;
                return {point, data, second, attack, dmg:{EDmg, EFeiDanDmg,EFeiDanDmgRea,QDmg,QDmgRea, eFeiDanDmgZhi,FourMinDmgZhi }}
            }));
        }
    })
    charactersDmg.sort((a,b)=>{
        return (b.point)-(a.point);
    });
    console.log(charactersDmg[1]);
    console.log(charactersDmg.map((v, n)=>{
        return  `(${n})得分: ${v.data.weaponname} : ${v.point.toFixed(5)}`
    }));
    console.log(count);
}

function getDMG(dataarr, getRet){
    let chance = CONFIG.chance;
    let _ret = [];
    for (let index = 0; index <= chance; index++) {
        let c1 = index; 
        for (let index2 = 0; index2 <= chance - index; index2++) {
            let c2 = index2;
            for (let index3 = 0; index3 <= chance-index-index2; index3++) {
                let c3 = index3;
                let c4 = chance - index - index2 - index3;
                let cs = [c1, c2, c3, c4];
                var tmpdata = {};
                CONFIG.clips.forEach((v, i) => {
                    tmpdata[v] = CLIPDATA[v] * cs[i]
                })
                _ret.push(tmpdata);
            }
        }
    }
    _ret = _ret.map(second => {
        let panel = objValueAdd([...dataarr, second]);
        return  getRet(panel, second)
    })
    _ret.sort((b,a)=> a.point-b.point)
    return _ret[0]
}

// 固定攻击力 19  防御力23 生命值299 5.3% 
function calc_clips_initial () {
    let clips = CONFIG.clips_initial.map(v => {
        let tmp = {};
        v.forEach(v2 => {
            tmp[v2] = CLIPDATA[v2]
        })
        return tmp
    })
    let data = {}
    clips.forEach(item => {
        for (const key in item) {
            data[key] = data[key]? (data[key]+item[key]) : item[key]
        }
    })
    return data
}
